package com.shuzijun.leetcode.editor.cn;
public class EditDistance{
    public static void main(String[] args) {
         Solution solution = new EditDistance().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int minDistance(String word1, String word2) {
        int n = word1.length();
        int m = word2.length();
        if (n == 0) return m;
        if (m == 0) return n;
        char[] w1 = word1.toCharArray();
        char[] w2 = word2.toCharArray();
        int[][] dp = new int[n + 1][m + 1];
        for (int i = 0; i <= m; i++) {
            dp[0][i] = i;
        }
        for (int i = 0; i <= n; i++) {
            dp[i][0] = i;
        }

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                if (w1[i-1] ==  w2[j-1]){
                    dp[i][j] = min3(dp[i-1][j]+1,dp[i][j-1]+1,dp[i-1][j-1]);
                }else {
                    dp[i][j] = min3(dp[i-1][j]+1,dp[i][j-1]+1,dp[i-1][j-1]+1);
                }
            }
        }
        return dp[n][m];
    }

        private int min3(int n1, int n2, int n3) {
            return Math.min(n1,Math.min(n2,n3));
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}